<?php 
/**
 * @file aa.php
 * @version 0.1
 * @desc 交易委托
 */
if(!defined('ACCOUNT_INIT'))
	exit('Access Denied!');

/* 定义全局变量 */
global $gLD;
$user_id = $gLD['user_id'];
$nick = $gLD['user_snick'];
$result = $gLD['sys_options'];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>交易委托</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<body oncontextmenu="window.event.returnValue=false">
<table width="617" height="482" border="0" cellpadding="6" cellspacing="0">
  <tr>
    <td valign="top">
<?php  //显示头部文件
    showHeader($gLD['module']); ?>
      <table width="605" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="379" height="436"><table width="371" height="100%" border="0" cellpadding="4" cellspacing="1" bgcolor="#012849">
            <tr>
              <td height="434" valign="top" bgcolor="#4484c1"><table width="361" border="0" cellspacing="0" cellpadding="0">
                <tr>
                  <td width="361" height="261" align="left" valign="top">
<!-- 买入K宝Begin -->
            
            
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="28" align="center" valign="bottom" bgcolor="#014075">
    	<table width="200" border="0" cellspacing="0" cellpadding="0">
        
<tr id="menuBuy">
  <td width="75" height="23" align="center" background="images/tab1.gif">
  	<strong>买入K宝</strong>
  </td>
  <td width="50" align="center">&nbsp;</td>
  <td width="75" align="center" background="images/tab2.gif">
  	<a href="#" id="clickSell">卖出K宝</a>
  </td>
</tr><!-- #menuSell -->
 
<tr id="menuSell">
    	<td width="75" height="23" align="center" background="images/tab2.gif">
    		<a href="#" id="clickBuy">买入K宝</a>
    	</td>
    <td width="50" align="center">&nbsp;</td>
    <td width="75" align="center" background="images/tab1.gif">
  	<strong>卖出K宝</strong>
    </td>
 </tr><!-- #menuBuy -->
        
    </table></td>
  </tr>
</table><!-- END MENU -->

<div id="kiss">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td height="235" align="center" valign="top" bgcolor="#3676B4"><table width="50%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                              <td>&nbsp;</td>
                            </tr>
                          </table>
                            <table width="90%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td width="18%" height="25" align="left"><span id="t1">买入</span>K宝：</td>
                                <td width="82%" align="left"><input id="knum" name="knum" type="text" size="20" maxlength="20" />
                                  个</td>
                              </tr>
                            </table>
                          <table width="90%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td width="18%" height="25" align="left">单 &nbsp;价：</td>
                                <td width="82%" align="left"><input id="kprice" name="kprice" type="text" size="20" maxlength="20" />
                                  K币/1K宝</td>
                              </tr>
                            </table>
                          <table width="90%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td width="18%" height="25" align="left"><span id="t2">花费</span>K币：</td>
                                <td width="82%" align="left"><input id="kcost" name="kcost" type="text" size="20" maxlength="20" readonly="true" />
                                  K币</td>
                              </tr>
                            </table>
                          <table width="90%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td width="18%" height="25" align="left">委托费：</td>
                                <td width="82%" align="left"><input id="ktax" name="ktax" type="text" size="20" maxlength="20" readonly="true" />
                                  K币<br />（<span id="taxNote1">委托费为 每K宝<?php echo $result['tax']; ?>K币</span>）</td>
                              </tr>
                            </table>
                          <table width="50%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td><span id="taxNote2">&nbsp;</span></td>
                              </tr>
                            </table>
                          <table width="90%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td align="left">说明：按下开始委托后，您的买单会被提交给系统作自由交易。<span id="error"> &nbsp; </span></td>
                              </tr>
                            </table>
                          <table width="50%" border="0" cellspacing="0" cellpadding="0">
                              <tr>
                                <td height="39" align="center" valign="bottom"><a href="#" id="kSubmit"><img src="images/kswt.gif" width="83" height="25" border="0" /></a></td>
                              </tr>
                          </table></td>
                      </tr>
                    </table>
       </div><!-- #kiss -->
<!-- 买入K宝End -->

					</td>
                </tr>
              </table>
                <table width="50%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td height="7"></td>
                  </tr>
                </table>
<?php require_once "left.php"; ?>
		</td>
            </tr>
          </table></td>
          <td width="226">
<?php require_once "top.php"; ?>
          </td>
        </tr>
      </table></td>
  </tr>
</table>


<script type="text/javascript">
$(document).ready(function(){
	getUserLeft(); //计算用户剩余K

	var clickCount = 0;
	var flag = 3; //默认买入K宝
	var q = 0; //为1时可以上载数据处理
	var kb = <?php echo $result['kb_public'] ?>; //官方公价
	var multiple = <?php echo $result['multiple']; ?>; //K币倍数
	var max = <?php echo $result['kb_max']; ?>; //K币上限
	var min = <?php echo $result['kb_min']; ?>; //K币下限
	var tax = <?php echo $result['tax']; ?>; //税率

	max = Number(kb)+Number(kb)*Number(max);
	min = Number(kb)-Number(kb)*Number(min);
	
	//菜单绑定--->卖出
	$("#clickSell").click(function(){
		$("#menuBuy").hide();
		$("#menuSell").show();
		$("#t1").replaceWith("<span id=\"t1\">卖出</span>");
		$("#t2").replaceWith("<span id=\"t2\">得到</span>");
		$("#kiss input").val(''); //置空输入框
		$("#taxNote2").replaceWith("<span id=\"taxNote2\" class=\"yellow\">该委托费将于成交后扣除.</span>");
		flag = 4;
		q = 0;
		$("#error").empty();
	});

	//菜单绑定--->卖入
	$("#clickBuy").click(function(){
		$("#menuSell").hide();
		$("#menuBuy").show();
		$("#t1").replaceWith("<span id=\"t1\">买入</span>");
		$("#t2").replaceWith("<span id=\"t2\">花费</span>");
		$("#kiss input").val('');
		$("#taxNote2").replaceWith("<span id=\"taxNote2\">&nbsp;</span>");
		flag = 3;
		q = 0;
		$("#error").empty();
	});

	//鼠标事件,移入input
	$("#kiss input").focus(function(){
		$(this).css('background',"#ccffff");
	});

	//鼠标事件,移出input
	$("#kiss input").blur(function(){
		$(this).css('background',"#fff");
	});

	//实时更新kcost 和 ktax
	$("#knum,#kprice").blur(function(){
		
		q = 0; //不能上载
		$("#kcost").val( '' );
		$("#ktax").val( '' );
		$("#error").empty();
		var knum = $("#knum").val();
		var kprice = $("#kprice").val();

		if( !isNull(knum) && !isNull(kprice) )  //都不为空
		if( isInteger(knum) && isInteger(kprice) ){
			if( Number(knum) == 0  ){
				$("#error").prepend("K宝不能为零！");
			}else if( Number(knum) < 0  ){
				$("#error").prepend("K宝不能为负数！");
			}else if( Number(kprice) < 0  ){
				$("#error").prepend("单价不能为负数！");
			}else if( Number(kprice)%Number(multiple) != 0 ){
				$("#error").prepend("单价必须是 "+multiple+ " 的倍数！");
			}else if ( Number(kprice) > Number(max) || Number(kprice) < Number(min) ){
				$("#error").prepend("单价必须在 "+min+"-"+max+ " 之间！");
			}else if ( flag == 3){
				var kcost = getCost(knum, kprice, false);
				if( Number(kcost) > Number( $("#ikbi-left").text() ) )
					$("#error").prepend("您的K币余额不足！ 需要 "+parseInt(kcost)+"K币");
				else q=1; //通过认证
			}else if ( flag == 4 ){
				//var ktax = Number(tax)*Number(knum);
				//alert( ktax );
				if( Number(knum) > Number( $("#ikbao-left").text() )  )
					$("#error").prepend("您的K宝余额不足！ 需要 "+knum+"K宝");
				//else if( Number(ktax) > Number( $("#ikbi-left").text() ) ){
				//	$("#error").prepend("您的K币余额不足！ 需要 "+ktax+"K币"); }
				else q=1; //通过认证
			}
		}else{
			$("#error").prepend("K宝数 和 单价 都必须是数字！");	
		} //认证结束

		if(q == 1){
			getCost(knum, kprice, true);
		}
	});

	//绑定“开始委托”按钮的点击时间
	$("#kSubmit").click(function(){
		//$(this).hide(); //点击后隐藏按钮
		var confirmB = true;
		if( Number(clickCount) > 0 )		
			confirmB = confirm("您刚刚已经提交了"+ clickCount +"个委托，是否需要继续委托？");

	if(confirmB == true )
		if(q == 1){
			clickCount = Number(clickCount) + 1;
			$.post("bg-bb.php", {
				action: 'trust',
				user_id: '<?php echo $user_id; ?>',
				flag: flag,
				knum: $("#knum").val(),
				kprice: $("#kprice").val()
				}, function(data, textStatus){
					if(data == 1){
						getUserLeft();
						alert("交易委托已经提交,系统正在处理...");
						$("#kiss input").val('');
						q = 0;
					}else if(data == '403'){
						alert("主账户登出K秀系统！");
						window.location='?uin=<?php echo $user_id; ?>&module=login';
					}else{
						alert("数据处理出错(ERROR:"+ data +")");
					}
				}, 'json');
		}else{
			var knum = $("#knum").val();
			var kprice = $("#kprice").val();
			if( isNull(knum) || isNull(kprice) )
				$("#error").prepend("  请输入K宝数 和 单价！");
			alert("您的输入有误，请看错误提示！");
		}
		
		//$(this).show(); //事件处理完成，显示按钮
	});

	// $("#kSubmit").ajaxStart(function(){ $(this).hide(); });
	// $("#kSubmit").ajaxStop(function(){ $(this).show(); }); 
	
	function getCost(num, price, f){
		var kcost = Number(num)*Number(price);
		var ktax = Number(num)*Number(tax);

		if( f == true ){
			$("#kcost").val( parseInt(kcost) );
			$("#ktax").val( parseInt(ktax) );
			return 0;
		}else{
			if(flag == 3){ //买入操作,加上tax
				kcost += Number(ktax);
			} 
			//else if(flag == 4){ //卖出操作,消耗K币0
				//kcost -= Number(ktax); }
			return kcost;
		}
	}//END getCost
	
	//刷新账户余额
	function getUserLeft(){
		$.post("bg-info.php", {
			action: 'getInfo',
			user_id: '<?php echo $user_id; ?>'
			}, function(data, textStatus){
				addInfoMsg( data );
			}, 'json');
	}//END func getUserLeft
	
	function addInfoMsg( data ){
		$("#ikbao-left").empty(); //交易账户
		$("#ikbi-left").empty();
		$("#ikbao-frozen").empty(); //冻结账户
		$("#ikbi-frozen").empty();
		$("#kbi-left").empty(); //本人账户
		$("#kbao-left").empty();
		
		$.each(data['info'], function(i,n){
			$("#ikbao-left").prepend(n['kbao2']); //交易账户
			$("#ikbi-left").prepend(n['kbi2']);
			$("#ikbao-frozen").prepend(n['kbao3']); //冻结账户
			$("#ikbi-frozen").prepend(n['kbi3']);		
			$("#kbi-left").prepend(n['kbi1']);   //prepend(n['kbi1']); //本人账户
			$("#kbao-left").prepend(n['kbao1']);
			});	
	} //END func addInfoMsg
	
}); //END $()

function isNull(vehicleid) {
	if(vehicleid == null || vehicleid == undefined || (!vehicleid))
		return true;
	else return false;
}

function isInteger(str) { 
	   if(/^(\-?)(\d+)$/.test(str)) return true; 
	   else return false; 
}
</script>
</body></html>